<?php
/**
 * Created by PhpStorm.
 * User: hao
 * Date: 2018/10/22
 * Time: 4:00 PM
 */

namespace app\api\validate;


use app\lib\exception\ParamterException;
use think\Request;
use think\Validate;

class BaseValidate extends Validate
{
    public function goCheck() {
        // 获取http传入参数
        //对参数做校验
        $request = Request::instance();
        $params = $request->param();

        $result = $this->check($params);
//        $result = $this->batch()->check($params); //批量验证
        if (!$result) {
            $e = new ParamterException([
                // $this->error有一个问题，并不是一定返回数组，需要判断
                'msg' => is_array($this->error) ? implode(
                    ';', $this->error) : $this->error,
            ]);
            throw $e;
        }else {
            return true;
        }
    }

    protected function idPostiveInteger($value,$rule='',$data='',$field='') {
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
            return true;
        } else {
            return false;
//            return $field.'必须是正整数';
        }
    }

    protected function isNotEmpty($value,$rule='',$data='',$field='') {
        if (empty($value)) {
            return false;
        }else {
            return true;
        }
    }

    //没有使用TP的正则验证，集中在一处方便以后修改
    //不推荐使用正则，因为复用性太差
    //手机号的验证规则
    protected function isMobile($value)
    {
        $rule = '^1(3|4|5|7|8)[0-9]\d{8}$^';
        $result = preg_match($rule, $value);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    public function getDataByRule($arrays) {
        if (array_key_exists('user_id',$arrays)|array_key_exists('uid',$arrays)) {
            throw new ParamterException(
                [
                    'msg' => '参数中包含非法的user_id或者uid'
                ]
            );
        }
        $newArray = [];
        foreach ($this->rule as $key => $value) {
            $newArray[$key] = $arrays[$key];
        }
        return $newArray;
    }
}